package com.service.dictionary;

import com.common.PojoUtil;
import com.common.ResultUtil;
import com.entity.OperationLog;
import com.entity.dictionary.Department;
import com.facade.dictionary.dept.IDepartmentFacadeCUD;
import com.facade.dictionary.dept.IDepartmentFacadeR;
import com.facade.log.IOperationLogFacadeCUD;
import com.facade.user.r.IManagerFacadeR;
import com.mapper.CustomMapper;
import com.mapper.dictionary.DepartmentMapper;
import com.service.CustomServiceCUD;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;

@Service
public class DepartmentServiceCUD extends CustomServiceCUD<Department> implements IDepartmentFacadeCUD {

    @Resource
    private DepartmentMapper mapper;
    @Resource
    private IDepartmentFacadeR departmentFacadeR;
    @Resource
    private IManagerFacadeR managerFacadeR;
    @Resource
    private IOperationLogFacadeCUD operationLogFacadeCUD;

    @Override
    protected CustomMapper<Department> initMapper() {
        return mapper;
    }

    @Override
    public ResultUtil addDepartment(Department department, Serializable... managerId) {
        ResultUtil resultUtil = ResultUtil.newInstance();
        Department _$0 = departmentFacadeR.findUniqueByPropertyEqualTo("name", department.getName());
        if (null != _$0) {
            resultUtil.setStatus(ResultUtil.PARAM_VALID_FAILED);
            return resultUtil.setMessage("部门名称已存在");
        }
        if (null == department.getSeq()) {
            department.setSeq(0);
        }
        addSelective(department);
        if (null != managerId && managerId.length > 0) {
            String message = "添加部门：[" + PojoUtil.notNull(department, "", "id") + "]";
            operationLogFacadeCUD.addOperationLog(managerId[0], OperationLog.ADD, message);
        }
        resultUtil.setMessage("添加成功");
        return resultUtil;
    }

    @Override
    public ResultUtil modifyDepartment(Department department, Serializable... managerId) {
        ResultUtil resultUtil = ResultUtil.newInstance();
        Long id = department.getId();
        if (null != id) {
            Department _$0 = departmentFacadeR.findByPrimaryKey(id);
            if (null == _$0) {
                resultUtil.setStatus(ResultUtil.FAILED);
                return resultUtil.setMessage("记录不存在或已被删除");
            }
            Department _$1 = departmentFacadeR.findUniqueByPropertyEqualTo("name", department.getName());
            if (null != _$1 && !_$1.getId().equals(id)) {
                resultUtil.setStatus(ResultUtil.PARAM_VALID_FAILED);
                return resultUtil.setMessage("部门名称已存在");
            }
            if (id.equals(department.getPid())) {
                resultUtil.setStatus(ResultUtil.PARAM_VALID_FAILED);
                return resultUtil.setMessage("上级部门不能为自身");
            }
            update(department);
            if (null != managerId && managerId.length > 0) {
                String message = "修改部门：[" + PojoUtil.compareDifferent(_$0, department, "", "id", "name") + "]";
                operationLogFacadeCUD.addOperationLog(managerId[0], OperationLog.MODIFY, message);
            }
            resultUtil.setMessage("修改成功");
        }
        return resultUtil;
    }

    @Override
    public ResultUtil deleteDepartment(Serializable id, Serializable... managerId) {
        ResultUtil resultUtil = ResultUtil.newInstance();
        Department department = departmentFacadeR.findByPrimaryKey(id);
        if (null == department) {
            resultUtil.setStatus(ResultUtil.FAILED);
            return resultUtil.setMessage("记录不存在或已被删除");
        }
        int count = managerFacadeR.countByPropertyEqualTo("departmentId", id);
        if (count > 0) {
            resultUtil.setStatus(ResultUtil.FAILED);
            resultUtil.setMessage("部门被使用不能删除");
            return resultUtil;
        }
        count = departmentFacadeR.countByPropertyEqualTo("pid", id);
        if (count > 0) {
            resultUtil.setStatus(ResultUtil.FAILED);
            resultUtil.setMessage("父部门不能直接删除");
            return resultUtil;
        }
        deleteByPrimaryKey(id);
        if (null != managerId && managerId.length > 0) {
            String message = "删除部门：[" + PojoUtil.notNull(department, "", "id", "name") + "]";
            operationLogFacadeCUD.addOperationLog(managerId[0], OperationLog.DELETE, message);
        }
        resultUtil.setMessage("删除成功");
        return resultUtil;
    }
}